package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Shader.TileMode;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.adapter.CityAndprovinceListAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.TextUtility;
import com.itboye.textileexpres.util.ListToStringUtil;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textillexepres.db.DBManager;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class ChinaIsCityAreaActivity extends BaseActivity implements
		OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private ArrayList<CityBeans> arrayLitArea;// 区域列表
	private DBManager dbManager;
	public String city;
	int type = 1;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	RelativeLayout relypoutGps;
	private TextView tvchina;
	String countryId;
	String Nametitle;
	static String from;
	String codeId;
TextView title;
	// listview头部
	TextView addTitle;
	String addhead;
	View view;
	CityBeans cityModel;
	private ListToStringUtil stringUtil;
	ImageView img_other;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		codeId = getIntent().getStringExtra("id");
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		img_other.setVisibility(View.GONE);
		from = getIntent().getStringExtra("from");
		title.setText(R.string.selectArea);
		// cityNoDBManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		// tvchina.setText(city);
		addhead = getIntent().getStringExtra("titlee");
		view = LayoutInflater.from(ChinaIsCityAreaActivity.this).inflate(
				R.layout.entry, null);
		addTitle = (TextView) view.findViewById(R.id.addTitle);
		addTitle.setText(addhead);
		country_lv_list.addHeaderView(view);
		mCityNames = getCityNames();
		// getCityUtility();
		cityModel = new CityBeans();
		// database.close();

		handler = new Handler();

		setAdapter(mCityNames);
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}


	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_province where countryid=" + codeId
						+ " ORDER BY province", null);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("province")));

			cityModel.setId(cursor.getString(cursor
					.getColumnIndex("provinceID")));

			names.add(cityModel);
		}
		return names;
	}

	
	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			if (pos == 0) {
				return;
			}
			searchType = true;
			cityModel = (CityBeans) country_lv_list.getAdapter().getItem(pos);
			city = cityModel.getCountry_name();
			cityModel.getCountryCode();

			String CityId = cityModel.getId();
			getPrivateNames(CityId);
			
			finish();
			country_lv_list.setSelection(-1);// 设置选择项为第一项

		}
	}

	@SuppressWarnings({ "unused", "static-access" })
	private String getPrivateNames(String codeId) {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();

		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_city where father=" + codeId
						+ " ORDER BY city", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("city")));

			cityModel.setCountryCode(cursor.getString(cursor
					.getColumnIndex("cityID")));
			names.add(cityModel);

		}

		if(cityModel.getId().equals("110000")||cityModel.getId().equals("310000")||cityModel.getId().equals("500000")||cityModel.getId().equals("120000")){
			Intent intent2 = new Intent("City");
			intent2.putExtra("from", addhead+from);
			sendBroadcast(intent2);
			SPUtils.put(ChinaIsCityAreaActivity.this, null,
					SPContants.COUNTRYNMAW, addhead+city);
		}else{
		Intent intent = new Intent(ChinaIsCityAreaActivity.this,
				GetProviceActivity.class);
		intent.putExtra("id", cityModel.getId());
		intent.putExtra("title", addhead+cityModel.getCountry_name());
		intent.putExtra("from", addhead+from);
		startActivity(intent);
		System.out.println("china Is CityId and area"
				+ cityModel.getCountryCode());
		}
		return stringUtil.ListToString(names);
	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new CityAndprovinceListAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}